home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / quicktime / goodies / offscreen.win / qtcode.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  14.3 KB  |  386 lines

  1. /*
  2.     File:        QTCode.c
  3.  
  4.     Contains:    QuickTime 3.0 Offscreen sample application.
  5.  
  6.     Written by:    Scott Kuechle
  7.  
  8.     Copyright:    © 1998 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.        <2>         5/18/98        rtm        changed QTCode_NewMovieFromWinPathname to use NativePathNameToFSSpec
  13.        <1>         4/24/98        srk        first file
  14.         
  15.  
  16.  NOTES:
  17.  
  18.  
  19.  TO DO:
  20.  
  21. */
  22.  
  23.  
  24. #include "QTCode.h"
  25.  
  26.  
  27. void QTCode_ReMapMovieBounds(Movie theMovie);
  28.  
  29.  
  30.  
  31.  
  32. /* ---------------------------------------------------------------- */
  33. /*                                                                  */
  34. /*    QTCode_CreateOffscreen()                                      */
  35. /*                                                                  */
  36. /*                                                                  */
  37. /*    Creates a Mac offscreen GWorld using the NewGWorldFromHBITMAP */
  38. /*    function                                                      */
  39. /*                                                                  */
  40. /*                                                                  */
  41. /* ---------------------------------------------------------------- */
  42.  
  43. QDErr QTCode_CreateOffscreen(GWorldPtr        *offscreenGWorld,
  44.                             CTabHandle         cTable,
  45.                             GDHandle         aGDevice,
  46.                             GWorldFlags     flags,
  47.                             HBITMAP            newHBITMAP,    /* a pointer to a valid bitmap or NULL */
  48.                             HDC                newHDC)        /* a pointer to a valid device context or NULL */
  49. {
  50.     QDErr err;
  51.  
  52.     /* pre-flight our input parameters */
  53.     if ( ((newHBITMAP == NULL) && (newHDC != NULL)) ||
  54.             ((newHBITMAP != NULL) && (newHDC == NULL)) )
  55.     {
  56.         return paramErr;
  57.     }
  58.     else
  59.     {
  60.         err = NewGWorldFromHBITMAP(offscreenGWorld,
  61.                                     cTable,            /*    CTabHandle         cTable        */
  62.                                     aGDevice,        /*    GDHandle         aGDevice    */
  63.                                     flags,            /*    GWorldFlags        flags        */
  64.                                     newHBITMAP,
  65.                                     newHDC);
  66.         return (err);
  67.     }
  68.  
  69. }
  70.  
  71. /* ---------------------------------------------------------------- */
  72. /*                                                                  */
  73. /*    QTCode_ForceMovieRedraw()                                     */
  74. /*                                                                  */
  75. /*                                                                  */
  76. /*    Invalidates a movies display state so subsequent calls to     */
  77. /*    MoviesTask will force the movie toolbox to redraw the movie   */
  78. /*                                                                  */
  79. /*                                                                  */
  80. /* ---------------------------------------------------------------- */
  81.  
  82. void QTCode_ForceMovieRedraw(Movie theMovie) 
  83. {
  84.     OSErr         err = noErr;
  85.     Rect        movieRect;
  86.     RgnHandle    clipRegion = NULL;
  87.  
  88.     
  89.         if (theMovie == NULL) goto bail;
  90.     
  91.         clipRegion = NewRgn();
  92.         if (clipRegion == NULL) goto bail;
  93.         
  94.         GetClip(clipRegion);
  95.         GetMovieBox(theMovie, &movieRect); 
  96.         ClipRect(&movieRect);
  97.     
  98.         UpdateMovie(theMovie);
  99.         MoviesTask(theMovie, 0);
  100.     
  101.         SetClip(clipRegion);
  102.     
  103.             /* Closure. Clean up if we have handles. */
  104.     bail:    
  105.     
  106.         if    (clipRegion != NULL)
  107.         {
  108.             DisposeRgn(clipRegion);
  109.         }
  110. }
  111.  
  112. /* ---------------------------------------------------------------- */
  113. /*                                                                  */
  114. /*    QTCode_SetMovieGWorld()                                       */
  115. /*                                                                  */
  116. /*                                                                  */
  117. /*    Sets the graphics world for displaying a movie                */
  118. /*                                                                  */
  119. /*                                                                  */
  120. /*                                                                  */
  121. /* ---------------------------------------------------------------- */
  122.  
  123. void QTCode_SetMovieGWorld(Movie theMovie, GWorldPtr offscreen) 
  124. {
  125.     SetMovieGWorld(theMovie, offscreen, GetGWorldDevice(offscreen));
  126. }
  127.  
  128. /* ---------------------------------------------------------------- */
  129. /*                                                                  */
  130. /*    QTCode_GetStartPointOfFirstVideoSample()                      */
  131. /*                                                                  */
  132. /*                                                                  */
  133. /*    Return the time value of the first video sample found in the  */
  134. /*    movie in the startpoint parameter. If the function fails,     */
  135. /*    startPoint will contain -1 and the OSErr is also returned     */
  136. /*                                                                  */
  137. /* ---------------------------------------------------------------- */
  138.  
  139. OSErr QTCode_GetStartPointOfFirstVideoSample(Movie        theMovie,
  140.                                             TimeValue    *startPoint) 
  141. {
  142.     OSErr    anErr = noErr;
  143.     OSType    media = VideoMediaType;
  144.     
  145.     *startPoint = -1;
  146.  
  147.     GetMovieNextInterestingTime(theMovie, nextTimeMediaSample+nextTimeEdgeOK, (TimeValue)1, &media, 0, 
  148.                                 fixed1, startPoint, NULL);
  149.     anErr = GetMoviesError();
  150.  
  151.     return anErr;
  152. }
  153.  
  154. /* ---------------------------------------------------------------- */
  155. /*                                                                  */
  156. /*    QTCode_NewMovieFromWinPathname()                              */
  157. /*                                                                  */
  158. /*                                                                  */
  159. /*    Creates a new movie, given a windows pathname to the desired  */
  160. /*    movie file location                                           */
  161. /*                                                                  */
  162. /*                                                                  */
  163. /* ---------------------------------------------------------------- */
  164.  
  165. OSErr QTCode_NewMovieFromWinPathname(char    *pathName,        /* c-string path to movie */
  166.                                     Movie    *theMovie,        /* Movie returned here */
  167.                                     short    *movieRefNum,    /* Movie RefNum returned here */
  168.                                     short    *movieResId)    /* Movie Resource ID returned here */
  169. {
  170.     FSSpec    fileSpec;
  171.     OSErr    err;
  172.  
  173.  
  174.     *theMovie = NULL;
  175.     *movieRefNum = 0;
  176.     *movieResId = 0;
  177.  
  178.     // Open up the movie file...
  179.  
  180.     // Fill in an FSSpec.
  181.     NativePathNameToFSSpec(pathName, &fileSpec, 0L);
  182.  
  183.     err = OpenMovieFile(&fileSpec, movieRefNum, fsRdWrPerm);
  184.     if (err)
  185.     {
  186.         err = OpenMovieFile(&fileSpec, movieRefNum, fsRdPerm);
  187.     }
  188.  
  189.     if (!err)
  190.     {
  191.         err = NewMovieFromFile(theMovie, *movieRefNum, movieResId, NULL, newMovieActive, NULL);
  192.     }
  193.  
  194.     if (err)
  195.     {
  196.         if (*theMovie)
  197.         {
  198.             DisposeMovie(*theMovie);
  199.         }
  200.     }
  201.  
  202.     return err;
  203. }
  204.  
  205. /* ---------------------------------------------------------------- */
  206. /*                                                                  */
  207. /*    QTCode_DoGetMovieNextInterestingTime()                        */
  208. /*                                                                  */
  209. /*                                                                  */
  210. /*    Gets the next time of interest in a given movie               */
  211. /*                                                                  */
  212. /*                                                                  */
  213. /*                                                                  */
  214. /* ---------------------------------------------------------------- */
  215.  
  216. TimeValue QTCode_DoGetMovieNextInterestingTime(TimeValue    currentTime,
  217.                                                 Movie        theMovie,
  218.                                                 OSType        mediaType)
  219. {
  220.     TimeValue nextTime = 0;
  221.  
  222.     GetMovieNextInterestingTime(theMovie, nextTimeMediaSample, 1, &mediaType, currentTime, 0, &nextTime, NULL);
  223.         /* have we reached the end of the movie? */
  224.     if(nextTime == -1)
  225.     {
  226.             /* end of movie, so go back to beginning */
  227.         GetMovieNextInterestingTime(theMovie, nextTimeMediaSample, 1, &mediaType, 1, 0, &nextTime, NULL);
  228.     }
  229.  
  230.     return nextTime;
  231. }
  232.  
  233. /* ---------------------------------------------------------------- */
  234. /*                                                                  */
  235. /*    QTCode_DoCreatePortAssociation()                              */
  236. /*                                                                  */
  237. /*                                                                  */
  238. /*    Create a mac graphics port and associate it with a given      */
  239. /*    window so QTML can draw into it                               */
  240. /*                                                                  */
  241. /*                                                                  */
  242. /* ---------------------------------------------------------------- */
  243.  
  244. GrafPtr QTCode_DoCreatePortAssociation(void *    theWnd,
  245.                                         Ptr     storage,
  246.                                         long     flags)
  247. {
  248.         /* Associate a GrafPort with this window */
  249.     return(CreatePortAssociation(theWnd, storage, flags));
  250. }
  251.  
  252. /* ---------------------------------------------------------------- */
  253. /*                                                                  */
  254. /*    QTCode_DoDestroyPortAssociation()                             */
  255. /*                                                                  */
  256. /*                                                                  */
  257. /*    De-register a mac graphics port and its associated            */
  258. /*    window                                                        */
  259. /*                                                                  */
  260. /*                                                                  */
  261. /* ---------------------------------------------------------------- */
  262.  
  263. void QTCode_DoDestroyPortAssociation(HWND hwnd)
  264. {
  265.         /* Destroy the port association */
  266.     DestroyPortAssociation((CGrafPtr)GetHWNDPort(hwnd));
  267. }
  268.  
  269. /* ---------------------------------------------------------------- */
  270. /*                                                                  */
  271. /*    QTCode_DoDestroyOffscreen()                                   */
  272. /*                                                                  */
  273. /*                                                                  */
  274. /*    Dispose the data structures associated with a mac offscreen   */
  275. /*    created with NewGWorlDFromHBITMAP                             */
  276. /*                                                                  */
  277. /*                                                                  */
  278. /* ---------------------------------------------------------------- */
  279.  
  280. void QTCode_DoDestroyOffscreen(GWorldPtr offscreen, HBITMAP    hBitmap)
  281. {
  282.     BOOL success;
  283.  
  284.         /* Destroy the offscreen GWorld and related objects */
  285.     DisposeGWorld(offscreen);
  286.     success = DeleteObject(hBitmap);
  287. }
  288.  
  289. /* ---------------------------------------------------------------- */
  290. /*                                                                  */
  291. /*    QTCode_DisposeMovieAndController()                            */
  292. /*                                                                  */
  293. /*                                                                  */
  294. /*    Disposes a movie and its associated controller                */
  295. /*                                                                  */
  296. /*                                                                  */
  297. /*                                                                  */
  298. /* ---------------------------------------------------------------- */
  299.  
  300. void QTCode_DisposeMovieAndController(Movie    theMovie,
  301.                                     short    movRefNum)
  302. {
  303.         // Dispose movie and controller
  304.     if (movRefNum)
  305.     {
  306.         CloseMovieFile(movRefNum);
  307.     }
  308.  
  309.     if (theMovie)
  310.     {
  311.         DisposeMovie(theMovie);
  312.     }
  313. }
  314.  
  315. /* ---------------------------------------------------------------- */
  316. /*                                                                  */
  317. /*    QTCode_DoQTInit()                                             */
  318. /*                                                                  */
  319. /*                                                                  */
  320. /*    Initialize Quicktime                                          */
  321. /*                                                                  */
  322. /*                                                                  */
  323. /*                                                                  */
  324. /* ---------------------------------------------------------------- */
  325.  
  326. BOOL QTCode_DoQTInit()
  327. {
  328.         /* Initialize QuickTime Media Layer */
  329.     if (InitializeQTML(0L) != 0)
  330.     {
  331.         return (FALSE);
  332.     }
  333.  
  334.         /* Initialize QuickTime */
  335.     if (EnterMovies() != 0)
  336.     {
  337.         return (FALSE);
  338.     }
  339.  
  340.     return (TRUE);
  341. }
  342.  
  343. /* ---------------------------------------------------------------- */
  344. /*                                                                  */
  345. /*    QTCode_QTCleanUp()                                            */
  346. /*                                                                  */
  347. /*                                                                  */
  348. /*    De-initialize Quicktime                                       */
  349. /*                                                                  */
  350. /*                                                                  */
  351. /*                                                                  */
  352. /* ---------------------------------------------------------------- */
  353.  
  354. void QTCode_QTCleanUp()
  355. {
  356.         /* Clean up */
  357.     ExitMovies();
  358.     TerminateQTML();
  359. }
  360.  
  361. /* ---------------------------------------------------------------- */
  362. /*                                                                  */
  363. /*    QTCode_PositionMovieRectInClientWindow()                      */
  364. /*                                                                  */
  365. /*                                                                  */
  366. /*    Adjusts the movie bounds of a given movie so the movie is     */
  367. /*    centered in the window                                        */
  368. /*                                                                  */
  369. /*                                                                  */
  370. /* ---------------------------------------------------------------- */
  371.  
  372. void QTCode_PositionMovieRectInClientWindow(Movie    theMovie,
  373.                                             HWND    hwnd)
  374. {
  375.     Rect movieBounds;
  376.  
  377.     GetMovieBox(theMovie, &movieBounds);
  378.  
  379.     CenterMovieRectInWindow(hwnd,                    /* window where we are placing the image */
  380.                             RECT_WIDTH(movieBounds), RECT_HEIGHT(movieBounds),    /* width, height, of the image */
  381.                             &movieBounds            /* on output, a Mac Rect centered in the window */
  382.                             );
  383.  
  384.     SetMovieBox(theMovie, &movieBounds);
  385. }
  386.